﻿package {
	import flash.display.Sprite;
	import flash.events.Event;
	[SWF(width="636", height="464")]

	public class StandingWave extends Sprite {
		public var wavefront: Number;
		public var wavelength: Number;
		public var color: uint;
		private var wave_x: Number;
		private var delta_x: Number;
		private var reflector: Number;
		private var speed: Number = 2;
		private var alph: Number = .3;
		private var i: uint = 0;
		private var controlpanel:ControlPanel;
		private var togglebutton:ToggleButton;

		public function StandingWave(wavefront:Number = 300, wavelength:Number = 70, color:uint=0xff0000) {
			this.wavefront = wavefront;
			this.wavelength = wavelength;
			this.color = color;

			init();
		}
		public function init():void {

			wave_x = wavefront;
			delta_x = 2;
			reflector = 420;

			controlpanel = new ControlPanel(stage.stageHeight, stage.stageWidth);
			addChild(controlpanel);
			togglebutton = new ToggleButton(stage.stageHeight);
			addChild(togglebutton);
			togglebutton.x = .9*stage.stageWidth;
			togglebutton.y = .91*stage.stageHeight;

			addEventListener(Event.ENTER_FRAME, onEnterFrame);

		}
		public function onEnterFrame(event:Event):void {

			if (wavefront < 2*reflector) {
				if (!controlpanel.pause) {wavefront += speed;}
				if (controlpanel.stepback) {wavefront -= speed/2;}
				if (controlpanel.stepforward) {wavefront += speed/2;}
				if (controlpanel.reset) {
					wavefront = 300;
					wave_x = wavefront;
					controlpanel.pause = true;
				}
				if(!togglebutton.up){
					alph = .4;
				}
				else{
					alph = 0;
				}
				drawWave(alph);


			} else {
				controlpanel.pause = true;
				if (controlpanel.reset) {
					wavefront = 300;
					wave_x = wavefront;
				}
			}
		}
		public function drawWave(alph:Number):void {

			graphics.clear();

			graphics.lineStyle(3, 0x000000, 1);
			graphics.moveTo(reflector, 100);
			graphics.lineTo(reflector, 300);

			graphics.lineStyle(1, 0x000000, 1);
			graphics.moveTo(reflector, 200);
			graphics.lineTo(0, 200);

			graphics.lineStyle(1, 0xff0000, 1);


			if (reflector > wavefront) {
				graphics.moveTo(reflector, 200);
				graphics.lineTo(wavefront, 200);

				for (i = 0; i < (wavefront/delta_x); i++) {
					wave_x = wavefront - i*delta_x;

					graphics.lineTo(wave_x, 200 + 20*Math.sin(Math.PI*2*(wave_x - wavefront)/wavelength));
				}
			} else {
				graphics.moveTo(reflector, 200 + 20*Math.sin(Math.PI*2*(reflector - wavefront)/wavelength));

				for (i = 0; i < (reflector/delta_x); i++) {
					if (wave_x > (2*reflector - wavefront)) {
						graphics.lineStyle(1, 0xff0000, alph);
					} else {
						graphics.lineStyle(1, 0xff0000, 1);
					}
					wave_x = reflector - i*delta_x;

					graphics.lineTo(wave_x, 200 + 20*Math.sin(Math.PI*2*(wave_x - wavefront)/wavelength));
				}
				wave_x = reflector;
				graphics.moveTo(reflector, 200 - 20*Math.sin(Math.PI*2*(reflector - wavefront)/wavelength));

				graphics.lineStyle(1, 0x00ff00, alph);

				for (i = 0; wave_x > (2*reflector - wavefront); i++) {
					wave_x = reflector - i*delta_x;
					graphics.lineTo(wave_x, 200 + 20*Math.sin(Math.PI*2*(wave_x - (2*reflector - wavefront))/wavelength));
				}
				graphics.moveTo(wave_x, 200 + 20*Math.sin(Math.PI*2*(wave_x - wavefront)/wavelength));
				graphics.lineStyle(1, 0x000000, 1);


				for (i = 0; wave_x < reflector; i++) {
					wave_x = (2*reflector - wavefront) + i*delta_x;
					graphics.lineTo(wave_x, 200 + 20*Math.sin(Math.PI*2*(wave_x - (2*reflector - wavefront))/wavelength)+ 20*Math.sin(Math.PI*2*(wave_x - wavefront)/wavelength));
				}
			}
		}
	}
}